//
//  TFListOfWinners.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/6.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFListOfWinners: TalkfunHitTestView, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TFListOfWinnersCell", for: indexPath) as! TFListOfWinnersCell
        cell.selectionStyle = .none
        cell.isUserInteractionEnabled = false
  
        let row:NSInteger =  indexPath.row
        cell.serialNumber.text = String.init(format: "%ld", row+1)
      
        let dict = self.list[indexPath.row] as! NSDictionary
        cell.name.text =  dict["nickname"] as? String

        let xid:String = dict["xid"] as! String
        if (xid == self.xid) {
            cell.name.textColor = UIColor(red: 87/255.0, green: 143/255.0, blue: 255/255.0, alpha: 1)
          
        }else{
            cell.name.textColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)
            
        }
       cell.serialNumber.textColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)
        return cell
    }
    
    @objc public let listOfWinnersTitle = UILabel()//标题: 中奖名单
    @objc public let line = UIView()//分割线
    @objc public let tableView = UITableView()
    @objc public var list = NSMutableArray()
    @objc public var xid:String = ""
    @objc public let closeWinners = TalkfunIncreaseButton()

    @objc public func exit()
    {
        self.isHidden = true
        self.list.removeAllObjects()
        self.tableView.reloadData()
    }
    @objc public func updateLanguage()

    {
        
        let attributes = [NSMutableAttributedString.Key.backgroundColor:UIColor.clear,          NSMutableAttributedString.Key.font:UIFont(name: "PingFang SC", size: 15) as Any,NSMutableAttributedString.Key.foregroundColor: UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0,alpha:1)]
        self.listOfWinnersTitle.attributedText = NSAttributedString.init(string:"中奖名单", attributes: attributes)
    }
    @objc public func reloadData(_ list:NSArray ,xid:String)

    {
        self.list.removeAllObjects()
       
        self.list.addObjects(from: list as! [Any])
        self.xid = xid
        self.tableView.reloadData()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //关闭中奖窗口
        self.closeWinners.addTarget(self, action: #selector(self.exit), for: .touchUpInside)
        self.closeWinners.setImage(UIImage.init(named: "HTLive.bundle/closeWinners"), for: .normal)
        self.addSubview(self.closeWinners)
           
   
        self.listOfWinnersTitle.numberOfLines = 0
        self.listOfWinnersTitle.textAlignment =  .center
        self.addSubview(listOfWinnersTitle)

        self.updateLanguage()
        
         listOfWinnersTitle.textColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0,alpha:1)
         listOfWinnersTitle.alpha = 1.0
           //TODO:分割线
         
        self.line.alpha = 1.0;
        self.line.backgroundColor = UIColor(red: 114/255.0, green: 119/255.0, blue: 129/255.0, alpha: 1)
      
        self.addSubview(line)
    
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.addSubview(self.tableView)
           //不要分割线  去线
        self.tableView.separatorStyle = .none
        self.tableView.separatorInset = .zero
          
           //设置背景景色
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.register(TFListOfWinnersCell.self, forCellReuseIdentifier: "TFListOfWinnersCell")
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.closeWinners.frame = CGRect(x: self.frame.size.width - 15 - 12, y: 15, width: 12, height: 12)
  
        self.listOfWinnersTitle.frame = CGRect(x: 0, y: 10, width: self.frame.size.width, height: 21)
        self.line.frame = CGRect(x: 0, y: 41, width: self.frame.size.width, height: 0.6)

        
        self.tableView.frame = CGRect(x: 0, y: self.line.frame.maxY, width: self.frame.size.width, height: self.frame.size.width-self.line.frame.maxY)
      
    }
    

}
